<template>
  <div class="me_mobile">
    <van-nav-bar
      title="我的"
    />
    <div v-if="!logined">
      <router-link to="login_mobile">登陆</router-link>
      <router-link to="register_mobile">注册</router-link>
    </div>
    <div v-if="logined">
      欢迎:{{nickname}}
      <van-button type="info" @click="logout">登出</van-button>
    </div>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  data() {
    return {
      logined: false,
      nickname: ""
    };
  },
  methods: {
    logout() {
      this.$api.get("logout/", {}, response => {
        if (response.status != 200) {
          this.$notify({ type: "danger", message: "操作失败，服务器错误" });
        } else {
          this.$notify({ type: "success", message: "操作成功" });
          this.logined = false;
        }
      });
    }
  },
  created: function() {
    this.$api.get("login/", {}, response => {
      if (response.status == 200) {
        var data = response.data;
        if (data.err_code==0 && data.data.result) {
          this.logined = true;
          this.nickname = data.data.nickname;
        }
      }
    });
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
